<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>智能水表抄表系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/styles/main.css">
</head>

<body>
    <header class="header">
        <h1><i class="fas fa-tint"></i> 智能水表抄表系统</h1>
    </header>
    
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history"></i> 历史记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/bills"><i class="fas fa-file-invoice-dollar"></i> 账单管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/anomalies"><i class="fas fa-exclamation-triangle"></i> 异常记录</a>
                    </li>
                    {% if user and user.role == "admin" %}
                    <li class="nav-item">
                        <a class="nav-link" href="/water_plans"><i class="fas fa-project-diagram"></i> 水务规划</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin"><i class="fas fa-user-shield"></i> 管理控制台</a>
                    </li>
                    {% endif %}
                </ul>
                <div class="navbar-nav">
                    {% if user %}
                    <span class="nav-item nav-link">
                        <i class="fas fa-user"></i> {{ user.username }}
                    </span>
                    <a class="nav-link" href="/profile"><i class="fas fa-user-cog"></i> 个人中心</a>
                    <a class="nav-link" href="/logout"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
                    {% else %}
                    <a class="nav-link" href="/login"><i class="fas fa-sign-in-alt"></i> 登录</a>
                    <a class="nav-link" href="/register"><i class="fas fa-user-plus"></i> 注册</a>
                    {% endif %}
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-4">
        {% if error %}
        <div class="alert alert-danger" role="alert">
            <i class="fas fa-exclamation-circle"></i> {{ error }}
        </div>
        {% endif %}

        <div class="row">
            <div class="col-lg-8 mx-auto">
                <!-- 上传卡片 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <i class="fas fa-camera"></i> 水表图像识别
                    </div>
                    <div class="card-body">
                        {% if image is defined and image %}
                            <img src="{{ image }}" alt="水表图片" class="img-preview mb-4">
                        {% else %}
                            <div class="empty-state mb-4">
                                <i class="fas fa-image"></i>
                                <p>尚未上传图片</p>
                            </div>
                        {% endif %}

                        <form action="/upload_image" method="post" enctype="multipart/form-data">
                            <div class="upload-area mb-3">
                                <i class="fas fa-cloud-upload-alt fs-3 mb-2 text-primary"></i>
                                <p>选择或拖放水表图片</p>
                                <input class="form-control" type="file" name="image" id="formFile">
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-custom-primary" {% if not user %}disabled{% endif %}>
                                    <i class="fas fa-search me-2"></i>上传并识别
                                </button>
                                {% if not user %}
                                <small class="text-muted text-center mt-2">需要登录后才能上传图片</small>
                                {% endif %}
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 识别结果 -->
                {% if number is defined and code is defined %}
                <div class="card mb-4">
                    <div class="card-header">
                        <i class="fas fa-check-circle"></i> 识别结果
                    </div>
                    <div class="card-body">
                        <div class="result-panel">
                            <div class="result-item">
                                <span class="result-label">水表读数:</span>
                                <span class="result-value">{{ number }}</span>
                            </div>
                            <div class="result-item">
                                <span class="result-label">水表编号:</span>
                                <span class="result-value">{{ code }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                {% endif %}

                <!-- 最新账单提示 -->
                {% if success is defined and latest_bill %}
                <div class="card mb-4">
                    <div class="card-header">
                        <i class="fas fa-file-invoice-dollar"></i> 最新账单
                    </div>
                    <div class="card-body">
                        <div class="result-panel">
                            <div class="result-item">
                                <span class="result-label">水表编码:</span>
                                <span class="result-value">{{ latest_bill.meter_code }}</span>
                            </div>
                            <div class="result-item">
                                <span class="result-label">用水量:</span>
                                <span class="result-value">{{ latest_bill.water_usage }} m³</span>
                            </div>
                            <div class="result-item">
                                <span class="result-label">账单金额:</span>
                                <span class="result-value text-danger">¥ {{ latest_bill.amount }}</span>
                            </div>
                            <div class="result-item">
                                <span class="result-label">状态:</span>
                                <span class="result-value">
                                    {% if latest_bill.status == 'paid' %}
                                    <span class="badge bg-success">已支付</span>
                                    {% else %}
                                    <span class="badge bg-warning">待支付</span>
                                    {% endif %}
                                </span>
                            </div>
                            <div class="d-flex justify-content-end gap-2 mt-3">
                                <a href="/bill/{{ latest_bill.id }}" class="btn btn-info">
                                    <i class="fas fa-eye"></i> 查看详情
                                </a>
                                {% if latest_bill.status == 'unpaid' %}
                                <a href="/pay/{{ latest_bill.id }}" class="btn btn-primary">
                                    <i class="fas fa-credit-card"></i> 立即支付
                                </a>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
                {% endif %}

                <!-- 最近记录 -->
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <div>
                            <i class="fas fa-history"></i> 最近抄表记录
                        </div>
                        {% if user %}
                        <a href="/history" class="btn btn-sm btn-custom-secondary">
                            <i class="fas fa-list"></i> 查看全部
                        </a>
                        {% endif %}
                    </div>
                    <div class="card-body">
                        {% if readings and readings|length > 0 %}
                        <div class="table-container">
                            <table class="custom-table">
                                <thead>
                                    <tr>
                                        <th>水表编号</th>
                                        <th>水表读数</th>
                                        <th>抄表时间</th>
                                        <th>图片</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for reading in readings[:5] %}
                                    <tr>
                                        <td>{{ reading.meter_code }}</td>
                                        <td>{{ reading.reading_value }}</td>
                                        <td>{{ reading.timestamp }}</td>
                                        <td>
                                            <a href="{{ reading.image_path }}" target="_blank">
                                                <img src="{{ reading.image_path }}" alt="水表图片" class="thumbnail">
                                            </a>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="empty-state">
                            <i class="fas fa-inbox"></i>
                            <p>暂无抄表记录</p>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="footer">
        <div class="container">
            <p>&copy; 2023 智能水表抄表系统 | 提供水表读数和编码识别</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>

</html>